package com.kingint.web.admin.base.controller;


import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasServiceClassService;
import com.kingint.mbg.bas.entity.BasServiceClass;

import java.util.List;


/**
 * @描述：服务内容表（服务工单中的服务内容）控制类
 * @作者: wxh
 * @日期: 2020-12-21
 */
@RestController
@RequestMapping("/basServiceClass")
@Api(value = "BasServiceClassController", description = "服务内容表（服务工单中的服务内容）")
@CrossOrigin
public class BasServiceClassController {

    @Autowired
    public BasServiceClassService basServiceClassService;


    @ApiOperation(value = "添加服务内容表（服务工单中的服务内容）")
    @PostMapping("/create")
    //@PreAuthorize("hasAuthority('basServiceClass:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasServiceClass basServiceClass) {
        int i = basServiceClassService.insertServiceClassInfo(basServiceClass);
        if (i > 0) {
            return K.ok().data(basServiceClass.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "更新服务内容表（服务工单中的服务内容）")
    @PutMapping("/update")
    // @PreAuthorize("hasAuthority('basServiceClass:update')")
    public Object update(@Validated(Update.class) @RequestBody BasServiceClass basServiceClass) {
        int i = basServiceClassService.updateServiceInfo(basServiceClass);
        if (i > 0) {
            return K.ok().data(basServiceClass.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "删除服务内容表（服务工单中的服务内容）")
    @DeleteMapping("/delete/{id}")
    // @PreAuthorize("hasAuthority('basServiceClass:delete')")
    public Object delete(@PathVariable("id") Integer id) {
        int i = basServiceClassService.deleteServiceClassInfo(id);
        if (i > 0) {
            return K.ok();
        } else {
            return K.error().message("此服务以绑定了分类");
        }
    }

    @ApiOperation(value = "获取服务和物品的分类信息")
    @GetMapping("/list")
    // @PreAuthorize("hasAuthority('basServiceClass:list')")
    public Object getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        return basServiceClassService.getBasServiceList(pageNum, pageSize);
    }

    @ApiOperation(value = "根据编号查询服务内容表（服务工单中的服务内容）信息")
    @GetMapping("/{id}")
    // @PreAuthorize("hasAuthority('basServiceClass:get')")
    public Object getItem(@PathVariable("id") Integer id) {
        BasServiceClass basServiceClass = basServiceClassService.getById(id);
        return K.ok().data(basServiceClass);
    }

    @ApiOperation(value = "获取相关的服务内容集合信息")
    @GetMapping("/getAllServiceClassList")
    public Object getAllServiceClassList() {
        List<BasServiceClass> basServiceClass = basServiceClassService.getAllServiceClassList();
        return K.ok().data(basServiceClass);
    }
}